package com.ins1st.modules.xs.web;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ins1st.core.R;
import com.ins1st.modules.xs.entity.Clazz;
import com.ins1st.modules.xs.service.ClazzService;
import com.ins1st.util.ControllerUtils;
import com.ins1st.util.QueryUtil;

@Controller
@RequestMapping("/xs/clazz")
public class ClazzAction {

	private static final String PREFIX = "modules/xs/";

	@Autowired
	private ClazzService clazzService;

	/**
	 * 主页
	 */
	@RequestMapping("index")
	// @NeedAuth("xs:clazz:index")
	public String index() {
		return PREFIX + "clazz_index.html";
	}

	/**
	 * 查询集合
	 */
	@RequestMapping("list")
	@ResponseBody
	public Object list(HttpServletRequest request) throws Exception {
		QueryWrapper<Clazz> query = new QueryWrapper<>();
		IPage page = QueryUtil.getPage(request);
		clazzService.page(page, query);
		
//		Clazz clazz = ControllerUtils.bindParams(request, Clazz.class);
//		QueryWrapper<Clazz> query = new QueryWrapper<>();
//		query.setEntity(clazz);
//		IPage<Clazz> page = clazzService.page(new Page<>(clazz.getPage(), clazz.getLimit()), query);
		return R.return4Page(page);
	}

	/**
	 * 添加页
	 */
	@RequestMapping("add")
	// @NeedAuth(value = "xs:clazz:add")
	public String add(Model model) {
		return PREFIX + "clazz_form.html";
	}

	/**
	 * 保存
	 */
	@RequestMapping("save")
	@ResponseBody
	public Object save(Clazz clazz) {
		boolean save = clazzService.saveOrUpdate(clazz);
		if (save) {
			return R.success("保存成功");
		}
		return R.error("保存失败");
	}

	/**
	 * 修改页
	 */
	@RequestMapping("get/{id}")
	// @NeedAuth("xs:clazz:edit")
	public String edit(@PathVariable Long id, Model model) {
		Clazz clazz = clazzService.getById(id);
		model.addAttribute("clazz", clazz);
		return PREFIX + "clazz_form.html";
	}

	/**
	 * 删除
	 */
	@RequestMapping("del/{id}")
	@ResponseBody
	public Object del(@PathVariable Long id) {
		boolean del = clazzService.removeById(id);
		if (del) {
			return R.success("删除成功");
		}
		return R.error("删除失败");
	}

}
